#include <iostream>
#include <string>
#include <vector>

void counts(std::string str_num, std::vector<int> &count_arr)
{
    for (int i = 0; i < str_num.size(); i++)
    {
        count_arr[str_num[i] - '0']++;
    }
}

int main()
{
    std::string str_num;
    int arr_size;
    std::cin >> str_num >> arr_size;

    // std::cout << str_num << " " << arr_size << std::endl;

    std::vector<int> count_arr(10, 0);
    counts(str_num, count_arr);

    int minv = -1;
    for (int i = 1; i < 1000 - arr_size; i++)
    {
        std::string tmp_num;
        for (int j = 0; j < arr_size; j++)
        {
            tmp_num += std::to_string(i + j);
        }
        std::vector<int> test_arr(10, 0);
        counts(tmp_num, test_arr);

        bool same = true;
        for (int k = 0; k < 10; k++)
        {
            if (count_arr[k] != test_arr[k])
            {
                same = false;
                break;
            }
        }
        if (same)
        {
            minv = i;
            break;
        }
    }
    std::cout << minv << std::endl;

    return 0;
}